package com.ophiux.FeverManagement.api.portImpl;

import java.lang.reflect.Type;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.ophiux.FeverManagement.api.port.IDoctorInfoPort;
import com.ophiux.FeverManagement.aplication.bean.doLoginDoctorRequest;
import com.ophiux.FeverManagement.aplication.bean.doLoginDoctorResonse;
import com.ophiux.FeverManagement.aplication.bean.doRegisterDoctorRequest;
import com.ophiux.FeverManagement.aplication.bean.doRegisterDoctorResponse;
import com.ophiux.FeverManagement.aplication.service.doctor.doLoginDoctorService;
import com.ophiux.FeverManagement.aplication.service.doctor.doRegisterDoctorService;
import com.ophiux.micro.core.application.Request;
import com.ophiux.micro.core.application.Result;
import com.ophiux.micro.core.application.ResultEnum;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class DoctorInfoPortImpl  implements IDoctorInfoPort {
	@Autowired
	doRegisterDoctorService doRegisterDoctorService;
	@Autowired
	doLoginDoctorService doLoginDoctorService;
	
	@Override
	public String doRegisterDoctor(String data) {
		log.info("doRegisterDoctor请求参数:---->"+data);
		Result<doRegisterDoctorResponse> result = null;
		Request<doRegisterDoctorRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<doRegisterDoctorRequest>>() {
					}.getType();
				}
			});
			
			result = doRegisterDoctorService.doRegisterDoctor(request.getPARAM());
			
		} catch (Exception e) {
			e.printStackTrace();
			log.info("doRegisterDoctor接口异常：",e);
			result = Result.fail(ResultEnum.requestException);
		}
		log.info("doRegisterDoctor接口,请求参数："+data+"\n doRegisterDoctor接口,返回参数："+ result.toJson());
		return result.toJson();
	}

	@Override
	public String doLoginDoctor(String data) {
		log.info("doLoginDoctor请求参数:---->"+data);
		Result<doLoginDoctorResonse> result = null;
		Request<doLoginDoctorRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<doLoginDoctorRequest>>() {
					}.getType();
				}
			});
			
			result = doLoginDoctorService.doLoginDoctor(request.getPARAM());
			
		} catch (Exception e) {
			e.printStackTrace();
			log.info("doLoginDoctor接口异常：",e);
			result = Result.fail(ResultEnum.requestException);
		}
		log.info("doLoginDoctor接口,请求参数："+data+"\n doLoginDoctor接口,返回参数："+ result.toJson());
		return result.toJson();
	}

}
